import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.AirPlane;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

public class AirPlaneController extends Controller
{
	private static final long	serialVersionUID	= 8701635252341035856L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		super.doGet(request, response);
	}

	public void index(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException
	{
		list(request, response);
	}

	public void list(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException
	{
		String replaceTheList = "<ul>";
		Criteria criteria = getSession().createCriteria(AirPlane.class);
		@SuppressWarnings("unchecked")
		ArrayList<AirPlane> airPlanes = (ArrayList<AirPlane>) criteria.list();
		for (AirPlane airPlane : airPlanes)
		{
			replaceTheList += "<a href=\"airplane?action=detailAirPlane&airplane=" + airPlane.getId() + "\">" + "<li>" + airPlane.getMark() + " " + airPlane.getMinEmployee() + "</li>" + "</a>";
		}
		replaceTheList += "</ul>";

		renderView(getViewHtml("airplane", "AirPlaneList").replaceAll("replaceTheList", replaceTheList), response);
	}

	public void newAirPlane(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException
	{
		String formHtml = initAirPlaneForm(null);
		String airPlaneNewHtml = getViewHtml("airplane", "AirPlaneNew");
		airPlaneNewHtml = airPlaneNewHtml.replace("replaceTheForm", formHtml);
		renderView(airPlaneNewHtml, response);
	}

	public void editAirPlane(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException
	{
		long airPlaneId = Long.parseLong(request.getParameter("airplane"));
		Criteria criteria = getSession().createCriteria(AirPlane.class);
		Criterion criterion = Restrictions.idEq(airPlaneId);
		criteria.add(criterion);

		AirPlane airPlane = (AirPlane) criteria.uniqueResult();

		String formHtml = initAirPlaneForm(airPlane);
		String airPlaneNewHtml = getViewHtml("airplane", "AirPlaneNew");
		airPlaneNewHtml = airPlaneNewHtml.replace("replaceTheForm", formHtml);
		renderView(airPlaneNewHtml, response);
	}

	public void detailAirPlane(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException
	{
		Criteria criteria = getSession().createCriteria(AirPlane.class);
		long airPlaneId = Long.parseLong(request.getParameter("airplane"));
		Criterion criterion = Restrictions.idEq(airPlaneId);
		criteria.add(criterion);
		AirPlane airPlane = (AirPlane) criteria.uniqueResult();
		String detailViewHtml = getViewHtml("airplane", "AirPlaneDetail");
		String detailHtml = "mark: " + airPlane.getMark() + "<br>";
		detailHtml += "minEmployee: " + airPlane.getMinEmployee() + "<br>";
		detailViewHtml = detailViewHtml.replace("replaceTheDetail", detailHtml);
		detailViewHtml = detailViewHtml.replace("replaceTheId", airPlane.getId() + "");
		renderView(detailViewHtml, response);
	}

	public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		try
		{
			// //transaction ////transaction =
			// getSession().begin////transaction();

			AirPlane airPlane;
			if ( request.getParameter("airplaneId").equals("-1") )
			{
				airPlane = new AirPlane();
			}
			else
			{
				long airPlaneId = Long.parseLong(request.getParameter("airplaneId"));
				airPlane = (AirPlane) getObjectByPk(AirPlane.class, airPlaneId);
			}
			airPlane.setMark(request.getParameter("mark"));
			airPlane.setMinEmployee(Integer.parseInt(request.getParameter("minEmployee")));

			getSession().saveOrUpdate(airPlane);
			// //transaction.commit();

			String destination = "/airplane?action=detailAirPlane&airplane=" + airPlane.getId();
			RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(destination);
			requestDispatcher.forward(request, response);
		}
		catch (Exception exception)
		{
			exception.printStackTrace();
			String destination = "/airplane?action=errorAirPlane";
			RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(destination);
			requestDispatcher.forward(request, response);
		}
		renderView(getViewHtml("airplane", "AirPlaneDetail"), response);
	}

	public String initAirPlaneForm(AirPlane airPlane)
	{
		String formHtml = getViewHtml("airplane", "AirPlaneForm");
		if ( airPlane == null )
		{
			formHtml = formHtml.replaceAll("replaceTheId", "-1");
			formHtml = formHtml.replaceAll("replaceTheMark", "");
			formHtml = formHtml.replaceAll("replaceTheMinEmployee", "");
			formHtml = formHtml.replaceAll("replaceTheSubmit", "add");
		}
		else
		{
			formHtml = formHtml.replaceAll("replaceTheId", "" + airPlane.getId());
			formHtml = formHtml.replaceAll("replaceTheMark", "" + airPlane.getMark());
			formHtml = formHtml.replaceAll("replaceTheMinEmployee", "" + airPlane.getMinEmployee());
			formHtml = formHtml.replaceAll("replaceTheSubmit", "save");
		}
		return formHtml;
	}
}
